from sklearn.model_selection  import train_test_split
from BigDataWeb.algorithm import Algorithm


# 分类算法(父类)
class Classifier(Algorithm):
    # 输出字段(即类别字段)
    output_field_name = ""
    # 训练及测试样本比例
    train_size = 0.8
    test_size = 0.2
    # 待预测数据
    predict_input_values = []
    predict_output_values = []
    predict_output_probas = []
    # 总样本(训练集+测试集)
    # inputs=train_inputs+test_inputs
    # outputs=train_outputs+test_outputs
    outputs = []
    # 训练集
    train_inputs = []
    train_outputs = []
    # 测试集
    test_inputs = []
    test_outputs = []
    # 评分(0~1)
    score = 0.0
    
    def setOutPutFieldName(self, output_field_name):
        # 区分输出
        self.output_field_name = output_field_name
        self.outputs = self.data_source[self.output_field_name].values
        
    def implent(self):
        # 执行算法
        # 拆分训练及测试样本
        (self.train_inputs, self.test_inputs, self.train_outputs, self.test_outputs) = train_test_split(self.inputs, self.outputs, train_size=self.train_size, test_size=self.test_size)
    
    def prepareIpynbItems(self):
        Algorithm.prepareIpynbItems(self)
        self.ipynb_items["#output_field_name#"] = self.output_field_name
        self.ipynb_items["#train_size#"] = self.train_size
        self.ipynb_items["#test_size#"] = self.test_size
        self.ipynb_items["#predict_input_values#"] = self.predict_input_values
